﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace Yichat.MicroService.Model
{
    public static class DistributeionCacheEntryExtensions
    {
        public static CacheItem FromCacheEntry<TValue>(this IDistributeionCacheEntry<TValue> cacheEntry)
        {
            if (cacheEntry == null)
                return null;

            return new CacheItem
            {
                Key = cacheEntry.Key,
                Value = JsonConvert.SerializeObject(cacheEntry.Value,
                new JsonSerializerSettings
                {
                    NullValueHandling = NullValueHandling.Ignore,
                }),
                CreatedUtc = cacheEntry.CreatedUtc,
                CacheExpireStategy = (int)cacheEntry.CacheExpireStategy,
                AbsoluteExpiration = cacheEntry.AbsoluteExpiration?.UtcDateTime,
                SlidingExpiration = (long)cacheEntry.SlidingExpiration?.TotalMilliseconds,
                LastAccessUtc = cacheEntry.LastAccessUtc,
            };
        }
    }
}
